/*
 * AgentModel.java
 * business
 *
 * Created by ChenTao on 2017/3/2.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.data.model;

import com.yidiandao.business.constant.Common;
import com.yidiandao.business.constant.Urls;
import com.yidiandao.business.data.bean.Goods;
import com.yidiandao.business.data.bean.Shop;
import com.yidiandao.business.data.request.OrderRequest;
import com.yidiandao.business.data.request.PagerRequest;
import com.yidiandao.business.data.response.AgentResponse;
import com.yidiandao.business.data.response.DeliveryResponse;
import com.yidiandao.business.manager.AgentsManager;
import com.yidiandao.business.data.net.BaseResponse;
import com.yidiandao.business.data.net.NetBiz;
import com.yidiandao.business.data.net.NetListener;
import com.yidiandao.business.data.net.exception.BizErrorInfo;
import com.yidiandao.business.data.net.exception.NetErrorInfo;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.listview.Functional;
import com.yidiandao.business.ui.widget.listview.displaylistitem.AgentInfoListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.AgentTitleListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.DeliveryListItem;
import com.yidiandao.business.utils.PreferenceUtils;
import com.yidiandao.business.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

/** 我是代理商Model */
public class AgentModel implements IAgentModel {

    private NetBiz mNetBiz = new NetBiz();

    private List<DisplayListItem> mAgentGoodsListItems = new ArrayList<>();

    private List<DisplayListItem> mDeliveryListItems = new ArrayList<>();

    private String lastPagerShopId;

    private int agentGoodsListPager, deliveryGoodsListPager;

    @Override
    public void loadAgentGoodList(
            final boolean isRefreshing, String tag, final OnGetAgentListener listener) {

        if (isRefreshing) {
            agentGoodsListPager = 1;
        }

        PagerRequest request = new PagerRequest();
        request.setUrl(Urls.PROXY_GOODS);
        request.setTag(tag);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setPage(agentGoodsListPager);
        request.setPageSize(Common.PAGER_SIZE);

        mNetBiz.sendGetRequest(request, AgentResponse.class, new NetListener<AgentResponse>() {
            @Override
            public void onSuccess(AgentResponse response) {

                agentGoodsListPager++;

                if (isRefreshing) {
                    mAgentGoodsListItems.clear();
                }

                // 代理商数据：包含 --> 店铺 、 店铺商品列表
                List<AgentResponse.Data.Agent> agents = response.getData().getAgent();

                for (int i = 0; i < agents.size(); i++) {

                    AgentResponse.Data.Agent agent = agents.get(i);

                    // 上一页最后一个 Shop --> id
                    if (i == agents.size() - 1) {
                        lastPagerShopId = agent.getShop().getSupplierId();
                    }

                    Shop shop = agent.getShop();
                    List<Goods> goodsList = agent.getGoodsList();

                    // 下拉刷新直接添加标题
                    if (isRefreshing) {
                        mAgentGoodsListItems.add(new AgentTitleListItem(shop));
                    } else {
                        // 加载更多
                        // 需要判定当前页的店铺标题与上一页最后一项的店铺标题是否相同
                        if (!shop.getSupplierId().equals(lastPagerShopId)) {
                            // 不相同则添加标题，相同则直接添加商品列表
                            mAgentGoodsListItems.add(new AgentTitleListItem(shop));
                        }
                    }

                    List<DisplayListItem> goodsListItems = Functional.serverToClient(
                            goodsList,
                            new Functional.Processor<Goods, DisplayListItem>() {
                                @Override
                                public DisplayListItem processToClient(
                                        Goods goods) {
                                    return new AgentInfoListItem(goods);
                                }
                            });

                    // 添加商品列表
                    mAgentGoodsListItems.addAll(goodsListItems);
                }

                if (listener != null) {
                    listener.onSuccess(mAgentGoodsListItems, mAgentGoodsListItems.size() == 0,
                            response.hasMoreData());
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return false;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {
                if (listener != null) {
                    listener.onFinal();
                }
            }
        });
    }

    @Override
    public void loadDeliveryList(
            final boolean isRefreshing, String tag, final OnGetAgentListener listener) {

        if (isRefreshing) {
            deliveryGoodsListPager = 1;
        }

        PagerRequest request = new PagerRequest();
        request.setPageSize(Common.PAGER_SIZE);
        request.setPage(deliveryGoodsListPager);
        request.setTag(tag);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setUrl(Urls.DELIVERY_ORDER);

        mNetBiz.sendGetRequest(request, DeliveryResponse.class,
                new NetListener<DeliveryResponse>() {
                    @Override
                    public void onSuccess(DeliveryResponse response) {

                        deliveryGoodsListPager++;

                        if (isRefreshing) {
                            mDeliveryListItems.clear();
                        }

                        DeliveryResponse.Data data = response.getData();
                        if (data != null) {
                            List<DeliveryResponse.Data.OrderListBean> orderList =
                                    data.getOrderList();
                            List<DisplayListItem> displayListItems = Functional.serverToClient(
                                    orderList,
                                    new Functional.Processor<
                                            DeliveryResponse.Data.OrderListBean,
                                            DisplayListItem>() {
                                        @Override
                                        public DisplayListItem processToClient(
                                                DeliveryResponse.Data.OrderListBean
                                                        orderListBean) {
                                            return new DeliveryListItem(orderListBean);
                                        }
                                    });
                            mDeliveryListItems.addAll(displayListItems);
                        }
                        if (listener != null) {
                            listener.onSuccess(
                                    mDeliveryListItems,
                                    mDeliveryListItems.size() == 0,
                                    response.hasMoreData());
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return false;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });
    }

    @Override
    public void handleEventFromDelivery(
            String tag, final int type, String orderId, final OnLoadNetListener listener) {
        final OrderRequest request = new OrderRequest();
        request.setTag(tag);
        request.setOrderId(orderId);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        //url
        switch (type) {
            case AgentsManager.TYPE.DISPATCH_GOODS:
                //一键发货
                request.setUrl(Urls.MEMBER_SEND_DELIVERY_ORDER);
                break;
            case AgentsManager.TYPE.RECEIVE_MONEY:
                //收款
                request.setUrl(Urls.MEMBER_PAY_DELIVERY_ORDER);
                break;
        }
        mNetBiz.sendGetRequest(request, BaseResponse.class, new NetListener<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                String msg = "";
                switch (type) {
                    case AgentsManager.TYPE.DISPATCH_GOODS:
                        //一键发货
                        msg = "发货成功";
                        break;
                    case AgentsManager.TYPE.RECEIVE_MONEY:
                        //收款
                        msg = "收款成功";
                        break;
                }
                if (null != listener) listener.onSuccess(msg);
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (null != listener) listener.onFailed();
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (null != listener) listener.onFailed();
                return true;
            }

            @Override
            public void onFinal() {
                if (null != listener) listener.onFinal();
            }
        });
    }
}
